package com.chushouya.manager.service.api.impl;

import com.general.framework.core.lang.Beans;
import com.chushouya.manager.dao.entity.KefuEntity;
import com.chushouya.manager.dao.repository.KefuRepository;
import com.chushouya.manager.dto.api.kefu.ReceiveDTO;
import com.chushouya.manager.service.api.KefuApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@Service
@Slf4j
public class KefuApiServiceImpl implements KefuApiService {


    @Resource
    private KefuRepository kefuRepository;

    @Override
    public ReceiveDTO kefuReceive() {
        List<KefuEntity> kefuList = kefuRepository.selectAll();
        //从客服列表中随机选择一个客服 status 1的
        
        // 过滤出状态为1且未删除的客服
        List<KefuEntity> availableKefuList = kefuList.stream()
                .filter(kefu -> kefu.getStatus() != null && kefu.getStatus() == 1)
                .collect(Collectors.toList());
        
        // 如果没有可用的客服，返回null
        if (availableKefuList.isEmpty()) {
            return null;
        }

        log.info("Available Kefu List: {}", availableKefuList);
        // 随机选择一个客服
        Random random = new Random();
        KefuEntity selectedKefu = availableKefuList.get(random.nextInt(availableKefuList.size()));

        return Beans.copy(selectedKefu, ReceiveDTO.class);
    }
}
